import QRequest from '@/service/index';
import { generateNameToLevel } from '@/config/constant';
import {
    MaterialPageResponse,
    MaterialPageParams,
    MaterialListInfo,
    MaterialSettingResponse,
    MaterialSetting
} from './model/type';

/**
 * @:获取物料列表
 * @param {MaterialPageParams} params
 * @return {*}
 */
export const doGetMaterialList = (params: MaterialPageParams) => {
    return QRequest.get<MaterialPageResponse>(`/material/selectAll`, {
        params
    });
};

/**
 * @: 删除物料
 * @param {number[]} params
 * @return {*}
 */
export const doDelMaterial = (data: number[]) => {
    return QRequest.delete(`/material/delete`, {
        data,
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 新增物料
 * @param {MaterialPageParams[]} data
 * @return {*}
 */
export const doAddMaterial = (data: MaterialListInfo[]) => {
    return QRequest.post(`/material/save`, data, {
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 编辑物料
 * @param {MaterialListInfo} data
 * @return {*}
 */
export const doUpdateMaterial = (data: MaterialListInfo) => {
    return QRequest.put(`/material/update`, data, {
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 查询物料分类设置
 * @param {MaterialSettingResponse} data
 * @return {*}
 */
export const doGetMaterialSetting = (params: MaterialSetting | boolean) => {
    return QRequest.get<MaterialSettingResponse>(`/materialClassification/selectAll`, {
        params: {
            size: 100,
            current: 1
        },
        headers: params
            ? {
                  loadingTarget: `#${generateNameToLevel('1')}`
              }
            : {
                  hideLoading: 1
              }
    });
};

/**
 * @: 查询物料单位
 * @param {MaterialSettingResponse} data
 * @return {*}
 */
export const doGetMaterialUnit = (params: MaterialPageParams | boolean) => {
    return QRequest.get<MaterialSettingResponse>(`/materialUnit/selectAll`, {
        params,
        headers: params
            ? {
                  loadingTarget: `#${generateNameToLevel('2')}`
              }
            : {
                  hideLoading: 1
              }
    });
};

/**
 * @: 新增物料分类设置
 * @param {MaterialSetting[]} data
 * @return {*}
 */
export const doAddMaterialSetting = (data: MaterialSetting[]) => {
    return QRequest.post<MaterialSettingResponse>(`/materialClassification/save`, data, {
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 新增物料单位
 * @param {MaterialSetting[]} data
 * @return {*}
 */
export const doAddMaterialUnit = (data: MaterialSetting[]) => {
    return QRequest.post<MaterialSettingResponse>(`/materialUnit/save`, data, {
        headers: {
            hideLoading: 1
        }
    });
};
/**
 * @: 修改物料分类设置
 * @param {MaterialSetting} data
 * @return {*}
 */
export const doEditMaterialSetting = (data: MaterialSetting) => {
    return QRequest.put(`/materialClassification/update`, data, {
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 修改物料单位
 * @param {MaterialSetting} data
 * @return {*}
 */
export const doEditMaterialUnit = (data: MaterialSetting) => {
    return QRequest.put(`/materialUnit/update`, data, {
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 删除物料分类设置
 * @param {number[]} params
 * @return {*}
 */
export const doDelMaterialSetting = (data: number[]) => {
    return QRequest.delete(`/materialClassification/delete`, {
        data,
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 删除物料单位
 * @param {number[]} params
 * @return {*}
 */
export const doDelMaterialUnit = (data: number[]) => {
    return QRequest.delete(`/materialUnit/delete`, {
        data,
        headers: {
            hideLoading: 1
        }
    });
};
